package name.sra.ui;

import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import name.sra.editor.ImageEditor;

public class PngEditorUI {
	
	private static final int SPACE = 20;
	JFrame frame;
	private BufferedImage bi;
	private JPanel mainPanel;
	private JPanel optionsPanel;
	private JPanel viewPanel;
	private double sensitivityLevel = 0.2;
	File file;
	private JLabel sensValue;

	public PngEditorUI(BufferedImage bi) {
		this.bi = bi;
	}
	
	public PngEditorUI(File file) throws Exception {
		this.file = file;
		BufferedImage candidateBI = ImageIO.read(file);
		this.bi = candidateBI;
	}
	
	public void createUI() {
	    frame = new JFrame();
	    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    
	    createMainPanel();
	    
	    frame.add(mainPanel);
	    frame.setMinimumSize(new Dimension(800, 150));
	    frame.setVisible(true);
	}

	private void createMainPanel() {
		
		mainPanel = new JPanel();
		BoxLayout layout = new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS);
		mainPanel.setLayout(layout);
		createOptionsPanel();
		mainPanel.add(Box.createRigidArea(new Dimension(0,SPACE)));
		createViewPanel();
	}

	private void createOptionsPanel() {
		optionsPanel = new JPanel();
		optionsPanel.setLayout(new BoxLayout(optionsPanel, BoxLayout.LINE_AXIS));
		createLeftOptionsPanel();
		optionsPanel.add(Box.createRigidArea(new Dimension(SPACE, 0)));
		createRightOptionsPanel();
		mainPanel.add(optionsPanel);
	}

	private void createLeftOptionsPanel() {
		JPanel panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
		JButton proceed = new JButton("proceed");
		panel.add(proceed);
		JButton save = new JButton("save");
		panel.add(save);
		optionsPanel.add(panel);
	}

	private void createRightOptionsPanel() {
		JPanel panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
		createSensitivityPanel(panel);
		optionsPanel.add(Box.createRigidArea(new Dimension(SPACE, 0)));
		createPinceauPanel(panel);
		optionsPanel.add(panel);
	}

	private void createSensitivityPanel(JPanel fatherPanel) {

		JPanel panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
		JLabel title = new JLabel("sensitivity");
		panel.add(title);
		JSlider sensitivityValue = new JSlider(JSlider.HORIZONTAL,
                										0, 100, 50);
		SensListener sensListener = new SensListener();
		sensitivityValue.addChangeListener(sensListener);
		panel.add(sensitivityValue);
		sensValue = new JLabel(Double.toString(sensitivityLevel));
		panel.add(sensValue);
		
		viewImage = new JButton("view");
		ImageEditor editor = new ImageEditor(file);
		SensitivityViewListener adapter = new SensitivityViewListener(editor);
		viewImage.addMouseListener(adapter);
		
		panel.add(viewImage);
		fatherPanel.add(panel);
	}

	private void createViewPanel() {
		viewPanel = new JPanel();
		viewPanel.setLayout(new BoxLayout(viewPanel, BoxLayout.PAGE_AXIS));
		mainPanel.add(viewPanel);
	}
	
	private JLabel coordinate;
	
	private void createPinceauPanel(JPanel fatherPanel) {
		JPanel panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
		JLabel title = new JLabel("taille de pinceau");
		panel.add(title);
		URL downImg = ClassLoader.getSystemResource("down.png");
		JButton down = new JButton(new ImageIcon(downImg));
		panel.add(down);
		URL upImg = ClassLoader.getSystemResource("up.png");
		JButton up = new JButton(new ImageIcon(upImg));
		panel.add(up);
		JLabel value = new JLabel("0");
		panel.add(value);
		
		panel.add(Box.createRigidArea(new Dimension(10, 0)));
		coordinate = new JLabel();
		panel.add(coordinate);
		
		fatherPanel.add(panel);
	}
	
	private JButton viewImage;
	
	private class SensitivityViewListener extends MouseAdapter {

		
		private ImageFrame imageFrame;

		public SensitivityViewListener(ImageEditor editor) {
			super();
		}

		ImageEditor editor;
		
		@Override
		public void mouseClicked(MouseEvent e) {
			if (!viewImage.isEnabled()) {
				return;
			}
			
			viewImage.setEnabled(false);
			BufferedImage img = null;
			editor = new ImageEditor(file);
			
			try {
				img = editor.eraseOverlappingClearPixelDownwards(sensitivityLevel);
				img = editor.eraseOverlappingClearPixelUpwards(sensitivityLevel);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			
			if (imageFrame != null) {
				imageFrame.dispose();
			}
			imageFrame = new ImageFrame(img);
			imageFrame.setLocation(frame.getX() + (frame.getWidth() - imageFrame.getWidth()) /2,
								   frame.getY() + frame.getHeight() + 20);
			imageFrame.setVisible(true);
			imageFrame.getPanel().addMouseListener(new MouseAdapter() {

				@Override
				public void mouseClicked(MouseEvent event) {
					super.mouseClicked(event);
					editor.eraseClearPixelZone(sensitivityLevel, event.getX(), event.getY());
					imageFrame.getPanel().repaint();
				}
			});
			viewImage.setEnabled(true);
		}

	}
	
	private class SensListener implements ChangeListener {
		public void stateChanged(ChangeEvent e) {
		    JSlider source = (JSlider)e.getSource();
		    if (!source.getValueIsAdjusting()) {
		        sensitivityLevel = ((double) source.getValue()) / 100;
		        sensValue.setText(Double.toString(sensitivityLevel));
		    }
		}
	}
}
